home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / • Extras • / SGI STL / xfunction.h < prev    next >
C/C++ Source or Header  |  1997-05-28  |  5KB  |  165 lines

  1. /*
  2.  * Copyright (c) 1997
  3.  * Moscow Center for SPARC Technology
  4.  *
  5.  * Permission to use, copy, modify, distribute and sell this software
  6.  * and its documentation for any purpose is hereby granted without fee,
  7.  * provided that the above copyright notice appear in all copies and
  8.  * that both that copyright notice and this permission notice appear
  9.  * in supporting documentation.  Moscow Center for SPARC Technology makes no
  10.  * representations about the suitability of this software for any
  11.  * purpose.  It is provided "as is" without express or implied warranty.
  12.  *
  13.  */
  14.  
  15. #ifndef __SGI_STL_XFUNCTION_H
  16. #define __SGI_STL_XFUNCTION_H
  17.  
  18. #include <function.h>
  19.  
  20. // This header provides various non-standard functional extensions
  21. // Some of them have to be specializations.
  22.  
  23. __BEGIN_STL_NAMESPACE
  24.  
  25.  
  26. // Extension : void function
  27.  
  28. // used as adaptor's return/argument type, 
  29. // to allow binders/composers usage
  30. # ifndef __VOID_TAG_DEFINED
  31. # define __VOID_TAG_DEFINED
  32.   struct __void_tag {};
  33. # endif
  34.  
  35. template <class Result>
  36. struct void_function {
  37.     typedef Result   result_type;
  38.     typedef __void_tag argument_type;
  39. };
  40.  
  41. template <class Result>
  42. class pointer_to_void_function : public unary_function<__void_tag,Result> {
  43. protected:
  44.     Result (*ptr)();
  45. public:
  46.     explicit pointer_to_void_function(Result (*x)()) : ptr(x) {}
  47.     Result operator()(__void_tag = __void_tag()) const { return ptr(); }
  48. };
  49.  
  50. // to feed composers
  51. template <class Arg1>
  52. struct projectvoid : public unary_function<Arg1,__void_tag> {
  53.   result_type operator()(const Arg1& x) const { return result_type(); }
  54. };
  55.  
  56. template <class Result>
  57. pointer_to_void_function<Result> ptr_fun(Result (*x)()) {
  58.     return pointer_to_void_function<Result>(x);
  59. }
  60.  
  61. // generators binding
  62.  
  63. template <class Operation, class Generator> 
  64. class binder1st_gen : 
  65. public unary_function<typename __BINARY_ARG(Operation,second_argument_type),
  66.                       typename __BINARY_ARG(Operation,result_type)> {
  67. protected:
  68.     Operation op;
  69.     Generator gen;
  70. public:
  71.     binder1st_gen(const Operation& x, const Generator& y) : op(x), gen(y) {}
  72.     result_type operator()(const argument_type& x) const {
  73.     return op(gen(),x); 
  74.     }
  75. };
  76.  
  77. template <class Operation,class Generator>
  78. inline binder1st_gen<Operation, Generator>
  79. bind1st_gen(const Operation& op, const Generator& gen)
  80. {
  81.     return binder1st_gen<Operation,Generator>(op,gen);
  82.  
  83. template <class Operation, class Generator> 
  84. class binder2nd_gen : 
  85. public unary_function<typename __BINARY_ARG(Operation,first_argument_type),
  86.                       typename __BINARY_ARG(Operation,result_type)> {
  87. protected:
  88.     Operation op;
  89.     Generator gen;
  90. public:
  91.     binder2nd_gen(const Operation& x, const Generator& y) : op(x), gen(y) {}
  92.     result_type operator()(const argument_type& x) const {
  93.     return op(x, gen()); 
  94.     }
  95. };
  96.  
  97. template <class Operation,class Generator>
  98. inline binder2nd_gen<Operation, Generator>
  99. bind2nd_gen(const Operation& op, const Generator& gen)
  100. {
  101.     return binder2nd_gen<Operation,Generator>(op,gen);
  102.  
  103. // 20.3.8  Adaptors for pointers to members [lib.member.pointer.adaptors]       
  104. // const versions for some compilers
  105. // normally, you won't need them. Names are non-standard, so beware.
  106.  
  107. template <class Class, class Result> 
  108. class mem_fun_const_ref_t : public unary_function<const Class, Result> {
  109. protected:
  110.     typedef Result (Class::*fun_type)(void) const;
  111.     fun_type ptr;
  112. public:
  113.     explicit mem_fun_const_ref_t(fun_type p) : ptr(p) {}
  114.     Result operator()(const Class& x) const { return (x.*ptr)();}
  115. };
  116.  
  117. template <class Class, class Arg, class Result>
  118. class mem_fun1_const_ref_t: public binary_function<const Class, Arg, Result> {
  119. public:
  120. protected:
  121.     typedef Result (Class::*fun_type)(Arg) const;
  122.     fun_type ptr;
  123. public:
  124.     explicit mem_fun1_const_ref_t(fun_type p) : ptr(p) {}
  125.     Result operator()(const Class& x, Arg a) const { return (x.*ptr)(a);}
  126. };
  127.  
  128. template <class Class, class Result>
  129. inline mem_fun_const_ref_t<Class, Result> 
  130. mem_fun_const_ref(Result (Class::*ptr)(void) const) {
  131.     return mem_fun_const_ref_t<Class, Result>(ptr);
  132. }
  133.  
  134. template <class Class, class Arg, class Result>
  135. inline mem_fun1_const_ref_t<Class, Arg, Result> 
  136. mem_fun1_const_ref(Result (Class::*ptr)(Arg) const) {
  137.     return mem_fun1_const_ref_t<Class, Arg, Result>(ptr);
  138. }
  139.  
  140. // macros to declare functional objects for pointers to members
  141. #define mem_fun_macro(Result,Class,Func) ¥
  142. struct : public unary_function<Class*,Result> ¥
  143. { Result operator()(Class* obj) const { return obj->Func(); }}
  144.  
  145. #define mem_fun1_macro(Result,Class,Func,Param) ¥
  146. struct : public binary_function<Class*, Param,Result>  ¥
  147. { Result operator()(Class* obj, Param p) const { return obj->Func(p); }}
  148.  
  149. #define mem_fun_ref_macro(Result,Class,Func) ¥
  150. struct : public unary_function<Class,Result> ¥
  151. { Result operator()(Class& obj) const { return obj.Func(); }}
  152.  
  153. #define mem_fun1_ref_macro(Result,Class,Func,Param) ¥
  154. struct : public binary_function<Class, Param,Result>  ¥
  155. { Result operator()(Class& obj, Param p) const { return obj.Func(p); }}
  156.  
  157. __END_STL_NAMESPACE
  158.  
  159. #endif
  160.  
  161.  
  162.  
  163.